﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Cache;
using DAL_Library;

namespace BLL_Library
{
    [Serializable()]
    public class RecipesMngr
    {
        /// <summary>
        /// Används för att spara och hämta/ladda information till applikationen från en fil
        /// </summary>
        private readonly RecipesDAL m_recipesDAL;

        /// <summary>
        /// Används för att bestämma vilken typ av sökning som ska göras - egenskap
        /// </summary> 
        public SearchEngine SearchEngine { get; set; }

        /// <summary>
        /// Listan med recept - egenskap
        /// </summary>
        public List<Recipe> RecipesList { get; set; }
 
        /// <summary>
        /// Standardkonstruktor
        /// </summary>
        public RecipesMngr()
        {
            RecipesList = new List<Recipe>();
            m_recipesDAL = new RecipesDAL();

            //InitializeTestData();
        }

        /// <summary>
        /// Lite testdata
        /// </summary>
        private void InitializeTestData()
        {
            var recipes = new Recipe();
            recipes.Ingredients.Add(new Ingredient() { MeasureType = MeasureType.dl, Name = "Socker", Quantity = 3 });
            recipes.Id = 1;
            recipes.Name = "Kladdkaka";
            recipes.Description = "Knäck ägg och ät skalen";
            recipes.Portions = 1;
            recipes.RecipeType = RecipeType.Efterätter;

            var recipes2 = new Recipe();
            recipes2.Ingredients.Add(new Ingredient() { MeasureType = MeasureType.dl, Name = "Nutella", Quantity = 3 });
            recipes2.Ingredients.Add(new Ingredient() { MeasureType = MeasureType.dl, Name = "Godis", Quantity = 3 });
            recipes2.Ingredients.Add(new Ingredient() { MeasureType = MeasureType.dl, Name = "Majs", Quantity = 3 });
            recipes2.Id = 2;
            recipes2.Name = "Mazariner";
            recipes2.Description = "Knäck ägg och ät skalen";
            recipes2.Portions = 2;
            recipes2.RecipeType = RecipeType.Efterätter;

            var recipes3 = new Recipe();
            recipes3.Ingredients.Add(new Ingredient() { MeasureType = MeasureType.dl, Name = "Socker", Quantity = 3 });
            recipes3.Id = 3;
            recipes3.Name = "Chokladbollar";
            recipes2.Description = "Knäck ägg och ät skalen";
            recipes3.Portions = 1;
            recipes3.RecipeType = RecipeType.Efterätter; 

            var recipes4 = new Recipe();
            recipes4.Ingredients.Add(new Ingredient() { MeasureType = MeasureType.dl, Name = "Socker", Quantity = 3 });
            recipes4.Id = 4;
            recipes4.Name = "Paj";
            recipes4.Description = "Knäck ägg och ät skalen";
            recipes4.Portions = 2;
            recipes4.RecipeType = RecipeType.Efterätter;

            var recipes5 = new Recipe();
            recipes5.Ingredients.Add(new Ingredient() { MeasureType = MeasureType.dl, Name = "Socker", Quantity = 3 });
            recipes5.Id = 5;
            recipes5.Name = "Chokladtårta";
            recipes5.Description = "Knäck ägg och ät skalen";
            recipes5.Portions = 2;
            recipes5.RecipeType = RecipeType.Efterätter;

            RecipesList.Add(recipes);
            RecipesList.Add(recipes2);
            RecipesList.Add(recipes3);
            RecipesList.Add(recipes4);
            RecipesList.Add(recipes5);
        }
     
        /// <summary>
        /// Hämtar alla recept
        /// </summary>
        /// <returns>Listan med recept så länge den inte är null</returns>
        public List<Recipe> GetRecipes()
        {
            return RecipesList ?? null;
        }

        /// <summary>
        /// Hämtar ett specifikt recept
        /// </summary>
        /// <returns>Ett receptobjekt</returns>
        public Recipe GetRecipe(int a_index)
        {
            return CheckIndex(a_index) ? RecipesList[a_index] : null;
        }
         

        /// <summary>
        /// Kollar så indexet finns
        /// </summary>
        public bool CheckIndex(int a_index)
        {
            return RecipesList != null && a_index < RecipesList.Count && a_index >= 0;
        }

        /// <summary>
        /// Ta bort ett recept
        /// </summary> 
        public void DeleteRecipe(int a_index)
        {
            if (CheckIndex(a_index))
            {
                RecipesList.RemoveAt(a_index);
            }
        }

        /// <summary>
        /// Lägger till ett recept
        /// </summary>
        public void Add(Recipe a_recipe)
        {
            if (a_recipe != null)
            {
                RecipesList.Add(a_recipe);
            }
        }

        /// <summary>
        /// Editerar ett recept
        /// </summary>
        public void EditRecipes(int a_index, Recipe a_recipe)
        {
            if (CheckIndex(a_index))
            {
                RecipesList.RemoveAt(a_index);
                RecipesList.Insert(a_index, a_recipe);
            }
        }

        /// <summary>
        /// Ladda applikationen med data från en fil
        /// </summary>
        public void LoadRecipes()
        {
            try
            {
                var list = m_recipesDAL.LoadRecipes();
                RecipesList = list.Cast<Recipe>().ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Spara ned datan i applikationen till en fil
        /// </summary>
        public void SaveRecipes()
        {
            try
            {
                if (RecipesList != null && RecipesList.Count > 0)
                {
                    m_recipesDAL.SaveRecipes(RecipesList.Cast<object>().ToList());
                } 
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        /// <summary>
        /// Bestämmer vilken sökmetod som ska användas
        /// </summary>
        /// <param name="searchMethod">Sökmetod</param>
        public void SetSearchMethod(SearchEngine searchMethod)
        {
            SearchEngine = searchMethod;
        }
    }
}
